/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 任务模块
	/// module : quest
	/// </summary>
	public interface SNetQuest : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.quest_acceptQuest, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.quest_acceptQuest, "Recv->" + data.ToString());
#endif
		                AcceptQuestRequest _data = (AcceptQuestRequest)data;
						return CS_acceptQuest(session, _data.cid);
		            } 
            	},
                { NetMessageId.quest_commitQuest, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.quest_commitQuest, "Recv->" + data.ToString());
#endif
		                CommitQuestRequest _data = (CommitQuestRequest)data;
						return CS_commitQuest(session, _data.cids, _data.batch);
		            } 
            	},
                { NetMessageId.quest_giveUpQuest, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.quest_giveUpQuest, "Recv->" + data.ToString());
#endif
		                GiveUpQuestRequest _data = (GiveUpQuestRequest)data;
						return CS_giveUpQuest(session, _data.cid);
		            } 
            	},
                { NetMessageId.quest_getPhaseReward, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.quest_getPhaseReward, "Recv->" + data.ToString());
#endif
		                GetPhaseRewardRequest _data = (GetPhaseRewardRequest)data;
						return CS_getPhaseReward(session, _data.cid, _data.phase);
		            } 
            	},
                { NetMessageId.quest_setTrackQuest, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.quest_setTrackQuest, "Recv->" + data.ToString());
#endif
		                SetTrackQuestRequest _data = (SetTrackQuestRequest)data;
						return CS_setTrackQuest(session, _data.cid);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.quest_acceptQuest, ()=> new AcceptQuestRequest()},
	        	{NetMessageId.quest_commitQuest, ()=> new CommitQuestRequest()},
	        	{NetMessageId.quest_giveUpQuest, ()=> new GiveUpQuestRequest()},
	        	{NetMessageId.quest_getPhaseReward, ()=> new GetPhaseRewardRequest()},
	        	{NetMessageId.quest_setTrackQuest, ()=> new SetTrackQuestRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        /// 接取任务
        /// </summary>
	    protected AcceptQuestResult CS_acceptQuest(ISession session, int cid);
		/// <summary>
        /// 提交任务--可批量
        /// </summary>
	    protected CommitQuestResult CS_commitQuest(ISession session, List<int> cids, bool batch);
		/// <summary>
        /// 放弃任务
        /// </summary>
	    protected GiveUpQuestResult CS_giveUpQuest(ISession session, int cid);
		/// <summary>
        /// 领取阶段奖励
        /// </summary>
	    protected GetPhaseRewardResult CS_getPhaseReward(ISession session, int cid, int phase);
		/// <summary>
        /// 设置追踪任务，cid为0表示取消
        /// </summary>
	    protected SetTrackQuestResult CS_setTrackQuest(ISession session, int cid);
#endregion
#region Client-side Interface
		/// <summary>
        /// Result只弹tips，不更新数据；notify中更新数据
        /// </summary>
        public AcceptQuestResult SC_acceptQuestResult(Task<AcceptQuestResult> future){return new AcceptQuestResult(future);}
        public AcceptQuestResult SC_acceptQuestResult(int code){return new AcceptQuestResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public CommitQuestResult SC_commitQuestResult(Task<CommitQuestResult> future){return new CommitQuestResult(future);}
        public CommitQuestResult SC_commitQuestResult(int code){return new CommitQuestResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public GiveUpQuestResult SC_giveUpQuestResult(Task<GiveUpQuestResult> future){return new GiveUpQuestResult(future);}
        public GiveUpQuestResult SC_giveUpQuestResult(int code){return new GiveUpQuestResult(code);}
		/// <summary>
        ///  成功时需更新任务数据
        /// </summary>
        public GetPhaseRewardResult SC_getPhaseRewardResult(Task<GetPhaseRewardResult> future){return new GetPhaseRewardResult(future);}
        public GetPhaseRewardResult SC_getPhaseRewardResult(int code, List<ItemShowPOD> awards){return new GetPhaseRewardResult(code, awards);}
        public GetPhaseRewardResult SC_getPhaseRewardResult(int code){return new GetPhaseRewardResult(code);}
		/// <summary>
        /// Result只弹tips
        /// </summary>
        public SetTrackQuestResult SC_setTrackQuestResult(Task<SetTrackQuestResult> future){return new SetTrackQuestResult(future);}
        public SetTrackQuestResult SC_setTrackQuestResult(int code){return new SetTrackQuestResult(code);}
		/// <summary>
        /// 通知接取任务 -- 插入到进行中列表，可能由服务器触发
        /// </summary>
		public bool SC_notifyAcceptQuest(ISession session, List<QuestPOD> quests){
	        return NetProtocolHelper.SendMessage(session, new NotifyAcceptQuest(quests));
	    }
	    public void SC_notifyAcceptQuest_Broadcast(List<ISession> sessions, List<QuestPOD> quests){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyAcceptQuest(quests));
	    }
		/// <summary>
        /// 更新任务信息
        /// </summary>
		public bool SC_notifyUpdateQuest(ISession session, QuestPOD questPOD){
	        return NetProtocolHelper.SendMessage(session, new NotifyUpdateQuest(questPOD));
	    }
	    public void SC_notifyUpdateQuest_Broadcast(List<ISession> sessions, QuestPOD questPOD){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyUpdateQuest(questPOD));
	    }
		/// <summary>
        /// 移除任务 -- 从进行列表中删除，可能由服务器触发
        /// </summary>
		public bool SC_notifyRemoveQuest(ISession session, List<int> cids){
	        return NetProtocolHelper.SendMessage(session, new NotifyRemoveQuest(cids));
	    }
	    public void SC_notifyRemoveQuest_Broadcast(List<ISession> sessions, List<int> cids){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyRemoveQuest(cids));
	    }
		/// <summary>
        /// 通知提交任务结果 -- 仅做展示
        /// </summary>
		public bool SC_notifyCommitQuest(ISession session, List<int> cids, List<ItemShowPOD> awards, QuestCommitOp op){
	        return NetProtocolHelper.SendMessage(session, new NotifyCommitQuest(cids, awards, op));
	    }
	    public void SC_notifyCommitQuest_Broadcast(List<ISession> sessions, List<int> cids, List<ItemShowPOD> awards, QuestCommitOp op){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyCommitQuest(cids, awards, op));
	    }
		/// <summary>
        /// 更新已提交的任务列表,remove: true表示从commitList中移除，否则表示添加
        /// </summary>
		public bool SC_notifyCommittedQuestList(ISession session, List<int> cids, bool remove){
	        return NetProtocolHelper.SendMessage(session, new NotifyCommittedQuestList(cids, remove));
	    }
	    public void SC_notifyCommittedQuestList_Broadcast(List<ISession> sessions, List<int> cids, bool remove){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyCommittedQuestList(cids, remove));
	    }
		/// <summary>
        /// 更新已失败的任务列表,remove: true表示从failedList中移除，否则表示添加
        /// </summary>
		public bool SC_notifyFailQuestList(ISession session, List<int> cids, bool remove){
	        return NetProtocolHelper.SendMessage(session, new NotifyFailQuestList(cids, remove));
	    }
	    public void SC_notifyFailQuestList_Broadcast(List<ISession> sessions, List<int> cids, bool remove){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyFailQuestList(cids, remove));
	    }
		/// <summary>
        /// 通知追踪任务信息
        /// </summary>
		public bool SC_notifyTrackQuest(ISession session, int cid){
	        return NetProtocolHelper.SendMessage(session, new NotifyTrackQuest(cid));
	    }
	    public void SC_notifyTrackQuest_Broadcast(List<ISession> sessions, int cid){
	        NetProtocolHelper.BroadcastMessage(sessions, new NotifyTrackQuest(cid));
	    }
#endregion

#region Messages
	public class AcceptQuestResult : MessageResult {
	    public AcceptQuestResult(Task<AcceptQuestResult> future): base(future) { }
		public AcceptQuestResult(int code): base(code) { }
		public new AcceptQuestResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<AcceptQuestResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.quest_acceptQuestResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_acceptQuestResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "Quest.SC_acceptQuestResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	public class CommitQuestResult : MessageResult {
	    public CommitQuestResult(Task<CommitQuestResult> future): base(future) { }
		public CommitQuestResult(int code): base(code) { }
		public new CommitQuestResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<CommitQuestResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.quest_commitQuestResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_commitQuestResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "Quest.SC_commitQuestResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	public class GiveUpQuestResult : MessageResult {
	    public GiveUpQuestResult(Task<GiveUpQuestResult> future): base(future) { }
		public GiveUpQuestResult(int code): base(code) { }
		public new GiveUpQuestResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<GiveUpQuestResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.quest_giveUpQuestResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_giveUpQuestResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "Quest.SC_giveUpQuestResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	public class GetPhaseRewardResult : MessageResult {
	    private List<ItemShowPOD> awards;
	    public GetPhaseRewardResult(Task<GetPhaseRewardResult> future): base(future) { }
		public GetPhaseRewardResult(int code): base(code) { }
		public new GetPhaseRewardResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal GetPhaseRewardResult(int code, List<ItemShowPOD> awards){
	        this.code = code;
	        this.awards = awards == null ? null : new (awards);
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<GetPhaseRewardResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.quest_getPhaseRewardResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_getPhaseRewardResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_list_pod(awards);
			return si_p;
		}
		public override string ToString() {
    		return "Quest.SC_getPhaseRewardResult { "
			+ "code="
			+ code
			+ ", awards="
			+ awards.ToString<ItemShowPOD>()
			+" }";
		}
	}
	public class SetTrackQuestResult : MessageResult {
	    public SetTrackQuestResult(Task<SetTrackQuestResult> future): base(future) { }
		public SetTrackQuestResult(int code): base(code) { }
		public new SetTrackQuestResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
        public override MessageResult GetFutureResult(Task task) { return ((Task<SetTrackQuestResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.quest_setTrackQuestResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_setTrackQuestResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			return si_p;
		}
		public override string ToString() {
    		return "Quest.SC_setTrackQuestResult { "
			+ "code="
			+ code
			+" }";
		}
	}
	public class NotifyAcceptQuest : MessageNotify {
		private List<QuestPOD> quests;
		public NotifyAcceptQuest(List<QuestPOD> quests) {
		    this.quests = quests == null ? null : new (quests);
		}
		public override int GetMessageId() {return NetMessageId.quest_notifyAcceptQuest;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_notifyAcceptQuest;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_list_pod(quests);
			return si_p;
		}
		public override string ToString() {
			return "Quest.SC_notifyAcceptQuest { "
			+ "quests="
			+ quests.ToString<QuestPOD>()
			+" }";
		}
	}
	public class NotifyUpdateQuest : MessageNotify {
		private QuestPOD questPOD;
		public NotifyUpdateQuest(QuestPOD questPOD) {
		    this.questPOD = questPOD;
		}
		public override int GetMessageId() {return NetMessageId.quest_notifyUpdateQuest;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_notifyUpdateQuest;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_pod(questPOD);
			return si_p;
		}
		public override string ToString() {
			return "Quest.SC_notifyUpdateQuest { "
			+ "questPOD="
			+ questPOD
			+" }";
		}
	}
	public class NotifyRemoveQuest : MessageNotify {
		private List<int> cids;
		public NotifyRemoveQuest(List<int> cids) {
		    this.cids = cids == null ? null : new (cids);
		}
		public override int GetMessageId() {return NetMessageId.quest_notifyRemoveQuest;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_notifyRemoveQuest;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_list_int(cids);	
			return si_p;
		}
		public override string ToString() {
			return "Quest.SC_notifyRemoveQuest { "
			+ "cids="
			+ cids.ToString<int>()
			+" }";
		}
	}
	public class NotifyCommitQuest : MessageNotify {
		private List<int> cids;
		private List<ItemShowPOD> awards;
		private QuestCommitOp op;
		public NotifyCommitQuest(List<int> cids, List<ItemShowPOD> awards, QuestCommitOp op) {
		    this.cids = cids == null ? null : new (cids);
		    this.awards = awards == null ? null : new (awards);
		    this.op = op;
		}
		public override int GetMessageId() {return NetMessageId.quest_notifyCommitQuest;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_notifyCommitQuest;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_list_int(cids);	
			_out.Write_list_pod(awards);
			_out.Write_byte((sbyte)op);
			return si_p;
		}
		public override string ToString() {
			return "Quest.SC_notifyCommitQuest { "
			+ "cids="
			+ cids.ToString<int>()
			+ ", awards="
			+ awards.ToString<ItemShowPOD>()
			+ ", op="
			+ op
			+" }";
		}
	}
	public class NotifyCommittedQuestList : MessageNotify {
		private List<int> cids;
		private bool remove;
		public NotifyCommittedQuestList(List<int> cids, bool remove) {
		    this.cids = cids == null ? null : new (cids);
		    this.remove = remove;
		}
		public override int GetMessageId() {return NetMessageId.quest_notifyCommittedQuestList;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_notifyCommittedQuestList;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_list_int(cids);	
			_out.Write_boolean(remove);
			return si_p;
		}
		public override string ToString() {
			return "Quest.SC_notifyCommittedQuestList { "
			+ "cids="
			+ cids.ToString<int>()
			+ ", remove="
			+ remove
			+" }";
		}
	}
	public class NotifyFailQuestList : MessageNotify {
		private List<int> cids;
		private bool remove;
		public NotifyFailQuestList(List<int> cids, bool remove) {
		    this.cids = cids == null ? null : new (cids);
		    this.remove = remove;
		}
		public override int GetMessageId() {return NetMessageId.quest_notifyFailQuestList;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_notifyFailQuestList;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_list_int(cids);	
			_out.Write_boolean(remove);
			return si_p;
		}
		public override string ToString() {
			return "Quest.SC_notifyFailQuestList { "
			+ "cids="
			+ cids.ToString<int>()
			+ ", remove="
			+ remove
			+" }";
		}
	}
	public class NotifyTrackQuest : MessageNotify {
		private int cid;
		public NotifyTrackQuest(int cid) {
		    this.cid = cid;
		}
		public override int GetMessageId() {return NetMessageId.quest_notifyTrackQuest;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.quest_notifyTrackQuest;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(cid);
			return si_p;
		}
		public override string ToString() {
			return "Quest.SC_notifyTrackQuest { "
			+ "cid="
			+ cid
			+" }";
		}
	}
	private class AcceptQuestRequest : IReceiveMessage
    {
		internal int cid;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            cid = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.quest_acceptQuest; }
        public override string ToString()
        {
            return "Quest.acceptQuest"+ ", cid="+ cid;
        }
    }
	private class CommitQuestRequest : IReceiveMessage
    {
		internal List<int> cids;
		internal bool batch;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            cids = _in.HasRemaining() ? _in.Read_list_int() : default;
            batch = _in.HasRemaining() ? _in.Read_boolean() : default;
		}
        public int GetMessageId() { return NetMessageId.quest_commitQuest; }
        public override string ToString()
        {
            return "Quest.commitQuest"+ ", cids="+ cids.ToString<int>()+ ", batch="+ batch;
        }
    }
	private class GiveUpQuestRequest : IReceiveMessage
    {
		internal int cid;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            cid = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.quest_giveUpQuest; }
        public override string ToString()
        {
            return "Quest.giveUpQuest"+ ", cid="+ cid;
        }
    }
	private class GetPhaseRewardRequest : IReceiveMessage
    {
		internal int cid;
		internal int phase;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            cid = _in.HasRemaining() ? _in.Read_int() : default;
            phase = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.quest_getPhaseReward; }
        public override string ToString()
        {
            return "Quest.getPhaseReward"+ ", cid="+ cid+ ", phase="+ phase;
        }
    }
	private class SetTrackQuestRequest : IReceiveMessage
    {
		internal int cid;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            cid = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.quest_setTrackQuest; }
        public override string ToString()
        {
            return "Quest.setTrackQuest"+ ", cid="+ cid;
        }
    }
	}
#endregion
}